<?php
declare (strict_types=1);

namespace app\admin\controller\recruit;

use app\common\model\AdminAdmin;
use app\common\model\RecruitCustomer;
use app\common\model\SchoolCustomerSource;
use app\common\model\StudyStudentClass;
use app\common\model\StudyStudentList;
use think\facade\Request;
use app\common\service\RecruitCustomer as S;
use app\common\model\RecruitCustomer as M;
use app\common\model\RecruitCustomerBody;

class Customer extends \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    // 列表
    public function index()
    {
        $id = session('admin.id');
        $admin = AdminAdmin::where('id', $id)->find();
        $list  = SchoolCustomerSource::field('id,source_info')
            ->where('school_id', $admin['school_id'])
            ->select();
        return $this->getAuto($this->fetch('',['list'=>$list]), M::getList());
    }

    // 添加
    public function add()
    {
        return $this->getAuto($this->fetch(), S::goAdd(Request::post()));
    }

    // 编辑
    public function edit($id)
    {
        return $this->getAuto($this->fetch('', ['model' => M::find($id)]), S::goEdit(Request::post(), $id));
    }

    // 状态
    public function status($id)
    {
        return $this->getJson(S::goStatus(Request::post('status'), $id));
    }

    // 删除
    public function remove($id)
    {
        return $this->getJson(S::goRemove($id));
    }

    // 批量删除
    public function batchRemove()
    {
        return $this->getJson(S::goBatchRemove(Request::post('ids')));
    }

    // 回收站
    public function recycle()
    {
        return $this->getAuto($this->fetch(), S::goRecycle());
    }

    public function finance()
    {
        return $this->getAuto($this->fetch(), M::getFinanceList());
    }

    public function financeRecruit($id)
    {
        return $this->getJson(S::financeRecruit(Request::post('finance_audit'), $id));
    }

    public function teacher()
    {
        return $this->getAuto($this->fetch(), M::getTeacherList());
    }

    public function childList()
    {
        $id = input('param.id');
        return $this->getAuto($this->fetch('', ['id' => $id]), M::getChildList());
    }

    public function studentAdd()
    {
        $id = input('param.id');
        $child = RecruitCustomerBody::where('id', $id)->find();
        $admin = session('admin');
        $cla = StudyStudentClass::where('school_id', $admin['school_id'])->select();
        return $this->getAuto($this->fetch('', ['model' => $child, 'cla' => $cla]), M::addChildStudent(Request::post(), $id));
    }


    public function student()
    {
        if ($this->request->isAjax()) {
            $admin = session('admin');
            $limit = input('get.limit');
            $username = input('get.username', '');
            $admin_id = AdminAdmin::where('school_id', $admin['school_id'])->column('id');
            $admin_id[] = $admin['id'];
            $list = RecruitCustomer::with([
                'get_admin' => function($a) {
                    $a->field('id,username');
                }
            ])->whereIn('admin_id', $admin_id)
                ->where('customer_type', 5)
                ->column('id');
            $body = RecruitCustomerBody::whereIn('customer_id', $list)
                ->where('student_id', '>', 0)
                ->column('student_id');

            $student = StudyStudentList::with([
                'get_class' => function($a) {
                    $a->field('id,class_name');
                }
            ])->field("id,username,sex,nursery_time,mobile,student_img,FROM_UNIXTIME(end_time,'%Y-%m-%d') as end_time,student_img,class_id")
                ->whereIn('id', $body)
                ->where('username', 'like','%'.$username.'%')
                ->paginate($limit);
            $data = $student->items();
            return ['code' => 0, 'data' => $data, 'extend' => ['count' => $student->total(), 'limit' => $limit]];
        } else {
            return $this->getAuto($this->fetch());
        }
    }

}
